// pages/address-administration/address-administration.js
const SERVICE = require("../../service.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this,
    td = this.data;
    let openid = wx.getStorageSync("token");    
    // 获取省份、城市、地区
    SERVICE.getLinkage({ openid},(status,data)=>{
      if(status){
        let province = data.data;
        SERVICE.getLinkage({ id: province[0].region_id, openid }, (status, data) => {
          if (status) {
            let city = data.data;
            SERVICE.getLinkage({ id: city[0].region_id, openid }, (status, data) => {
              if (status) {
                let county = data.data;
                _this.setData({
                  province,
                  city,
                  county
                });
              }
            });
          }
        });
      }
    });

    if (options.updateSiteId){
      let updateSiteId = options.updateSiteId;
      let openid = wx.getStorageSync("token");      
      SERVICE.getUpdateAddress({ id: updateSiteId,openid},(status,data)=>{
       if(status){
         let updateSiteData = data.data;
         console.log(updateSiteData);
         _this.setData({
           updateSiteId,
           updateSiteData,
           Province: updateSiteData.province,
           City: updateSiteData.city,
           District: updateSiteData.district,
           siteData: updateSiteData.province2 + '/' + updateSiteData.city2 + '/' +updateSiteData.district2
         });
       }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  mobile(e){
    let mobile_val = e.detail.value;
    if (mobile_val.length > 0 && checkPhone(mobile_val) == false){
      wx.showToast({
        title: "号码格式不正确",
        icon:"none",
        mask:true
      });
    }
  },
  site(){
    this.setData({
      showArea:true
    });
  },
  
  AreaConfirm(e){
    let provinceItem = e.detail.province,
      cityItem = e.detail.city,
      countyItem = e.detail.county;
    this.setData({
      siteData: provinceItem.region_name + '/' + cityItem.region_name + '/' + countyItem.region_name,
      provinceId: provinceItem.region_id,
      cityId: cityItem.region_id,
      countyId: countyItem.region_id,
      showArea:false
    });
    
  },
  formSubmit(e){
    let td = this.data,
      mobile = e.detail.value.mobile,
      name = e.detail.value.name,
      site = e.detail.value.site,
      address_details = e.detail.value.addressdetails;  
    console.log(e);
    let openid = wx.getStorageSync("token");                  
    if (name.length == 0){
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        mask: true
      });
    } else if (mobile.length == 0){
      wx.showToast({
        title: "请输入手机号",
        icon: "none",
        mask: true
      });
    } else if (mobile.length > 0 && checkPhone(mobile) == false){
      wx.showToast({
        title: "号码格式不正确",
        icon: "none",
        mask: true
      });
    } else if (site.length == 0){
      wx.showToast({
        title: "请选择所在地区",
        icon: "none",
        mask: true
      });
    } else if (address_details.length == 0){
      wx.showToast({
        title: "请输入详细地址",
        icon: "none",
        mask: true
      });
    }else{
      if (td.updateSiteId){
        console.log(td.province);
        SERVICE.updateAddress({
          address_id: td.updateSiteId,
          province: td.provinceId || td.Province,
          city: td.cityId || td.City,
          district: td.countyId || td.District,
          address: address_details,
          consignee: name,
          mobile: mobile,
          openid: openid
        },(status,data)=>{
          wx.showToast({
            title: data.message,
            icon: "none",
            mask: true
          });
          if (status) {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          }
        });
      }else{
        SERVICE.addAddress({
          province: td.provinceId,
          city: td.cityId,
          district: td.countyId,
          address: address_details,
          consignee: name,
          mobile: mobile,
          openid: openid
        }, (status, data) => {
          wx.showToast({
            title: data.message,
            icon: "none",
            mask: true
          });
          if (status) {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          }
        });
      }
    }
  }
})

// 检验手机号
function checkPhone(val) {
  if (!(/^1[34578]\d{9}$/.test(val))) {
    return false;
  }
} 

